package pl.edu.agh.jemo.gui.panels.crossover;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.operator.common.DomainControl;
import pl.edu.agh.jemo.evolution.operator.crossover.ClassicCrossover;

/**
 * Configuration panel allowing to adjust classic crossover parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing crossover instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class ClassicCrossoverConfigPanel extends JPanel {
	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 7989267584287856224L;
	
	/**
	 * Field allowing to set the way specimen out of domain are handled.
	 */
	protected JComboBox domainControlCombo;
	/**
	 * Field allowing to set chance for single crossover to happen.
	 */
	protected JTextField crossoverChanceEdit;
	/**
	 * Field allowing to set if new specimen is returned even if worse than parent.
	 */
	protected JCheckBox improveSpecimenCheck;
	
	/**
	 * Reference to crossover instance configured by panel.
	 */
	private ClassicCrossover crossover;

	/**
	 * Creates Panel instance associated with given crossover reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param crossoverReference reference to classic crossover, that needs configuration. 
	 */
	public ClassicCrossoverConfigPanel(ClassicCrossover crossoverReference) {
		crossover = crossoverReference;
		
		JLabel domainLabel = new JLabel("Domain control policy:");
		domainLabel.setBounds(10, 10, 150, 15);
		DomainControl[] controlValues = { DomainControl.GENERATE_NEXT, DomainControl.IGNORE, DomainControl.MOVE_TO_BORDER };
		domainControlCombo = new JComboBox(controlValues);
		domainControlCombo.setSelectedItem(crossover.getDomainControl());
		domainControlCombo.setBounds(10, 30, 150, 25);
		domainControlCombo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				crossover.setDomainControl((DomainControl)(domainControlCombo.getSelectedItem()));
			}
		});
		
		JLabel chanceLabel = new JLabel("Crossover chance:");
		chanceLabel.setBounds(10, 70, 150, 15);
		crossoverChanceEdit = new JTextField(String.valueOf(crossover.getCrossoverChance()));
		crossoverChanceEdit.setBounds(10, 90, 150, 20);
		crossoverChanceEdit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					crossover.setCrossoverChance(Double.parseDouble(crossoverChanceEdit.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
		
		improveSpecimenCheck = new JCheckBox("Ensure specimen improvement");
		improveSpecimenCheck.setSelected(crossover.isImproveSpecimen());
		improveSpecimenCheck.setBounds(10, 120, 150, 15);
		improveSpecimenCheck.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				crossover.setImproveSpecimen(improveSpecimenCheck.isSelected());
			}
		});
		
		add(domainLabel);
		add(domainControlCombo);
		add(chanceLabel);
		add(crossoverChanceEdit);
		add(improveSpecimenCheck);
		
	}

}
